
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 09/01/2013 18:09:07
-- Generated from EDMX file: C:\liva\work\codesample\caedisi\caedisi.Web\DataModel\CellularAutomataDataModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [CADB];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_CellCellStateType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Cell] DROP CONSTRAINT [FK_CellCellStateType];
GO
IF OBJECT_ID(N'[dbo].[FK_CellularAutomataCell]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Cell] DROP CONSTRAINT [FK_CellularAutomataCell];
GO
IF OBJECT_ID(N'[dbo].[FK_CellularAutomataNeighbourhoodType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CellularAutomata] DROP CONSTRAINT [FK_CellularAutomataNeighbourhoodType];
GO
IF OBJECT_ID(N'[dbo].[FK_CellularAutomataTransitionRule]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TransitionRule] DROP CONSTRAINT [FK_CellularAutomataTransitionRule];
GO
IF OBJECT_ID(N'[dbo].[FK_NeighbourhoodStateConfigurationTransitionRule]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NeighbourhoodStateConfiguration] DROP CONSTRAINT [FK_NeighbourhoodStateConfigurationTransitionRule];
GO
IF OBJECT_ID(N'[dbo].[FK_TransitionRuleCellStateTypeCurrent]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TransitionRule] DROP CONSTRAINT [FK_TransitionRuleCellStateTypeCurrent];
GO
IF OBJECT_ID(N'[dbo].[FK_TransitionRuleCellStateTypeResult]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TransitionRule] DROP CONSTRAINT [FK_TransitionRuleCellStateTypeResult];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Cell]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Cell];
GO
IF OBJECT_ID(N'[dbo].[CellStateType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CellStateType];
GO
IF OBJECT_ID(N'[dbo].[CellularAutomata]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CellularAutomata];
GO
IF OBJECT_ID(N'[dbo].[Configuration]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Configuration];
GO
IF OBJECT_ID(N'[dbo].[NeighbourhoodStateConfiguration]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NeighbourhoodStateConfiguration];
GO
IF OBJECT_ID(N'[dbo].[NeighbourhoodType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NeighbourhoodType];
GO
IF OBJECT_ID(N'[dbo].[TransitionRule]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TransitionRule];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'CellStateTypes'
CREATE TABLE [dbo].[CellStateTypes] (
    [CellStateTypeId] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [AliasCharacter] nvarchar(max)  NOT NULL,
    [Color] nvarchar(max)  NOT NULL,
    [IsSystemState] bit  NOT NULL
);
GO

-- Creating table 'CellularAutomatas'
CREATE TABLE [dbo].[CellularAutomatas] (
    [CellularAutomataId] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Url] nvarchar(max)  NULL,
    [Width] smallint  NOT NULL,
    [Height] smallint  NOT NULL,
    [NeighbourhoodTypeId] int  NOT NULL,
    [RadiusVisibility] int  NOT NULL,
    [RadiusContamination] int  NOT NULL,
    [ImmunityTime] int  NOT NULL,
    [CircularHorizontal] bit  NOT NULL,
    [CircularVertical] bit  NOT NULL
);
GO

-- Creating table 'Configurations'
CREATE TABLE [dbo].[Configurations] (
    [ConfigurationId] int IDENTITY(1,1) NOT NULL,
    [key] nvarchar(max)  NOT NULL,
    [Value] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL
);
GO

-- Creating table 'NeighbourhoodStateConfigurations'
CREATE TABLE [dbo].[NeighbourhoodStateConfigurations] (
    [NeighbourhoodStateConfigurationId] int IDENTITY(1,1) NOT NULL,
    [TransitionRuleId] int  NOT NULL,
    [CellStateTypeId] int  NOT NULL,
    [x_offset] int  NOT NULL,
    [y_offset] int  NOT NULL,
    [z_offset] int  NOT NULL
);
GO

-- Creating table 'NeighbourhoodTypes'
CREATE TABLE [dbo].[NeighbourhoodTypes] (
    [NeighbourhoodTypeId] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Url] nvarchar(max)  NULL,
    [IsTopLeftON] bit  NOT NULL,
    [IsTopON] bit  NOT NULL,
    [IsTopRightON] bit  NOT NULL,
    [IsLeftON] bit  NOT NULL,
    [IsRightON] bit  NOT NULL,
    [IsBottomLeftON] bit  NOT NULL,
    [IsBottomON] bit  NOT NULL,
    [IsBottomRightON] bit  NOT NULL
);
GO

-- Creating table 'TransitionRules'
CREATE TABLE [dbo].[TransitionRules] (
    [TransitionRuleId] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [Url] nvarchar(max)  NULL,
    [CellularAutomataId] int  NOT NULL,
    [CurrentCellStateTypeId] int  NOT NULL,
    [ResultCellStateTypeId] int  NOT NULL,
    [NeighbouringStateConfiguration] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Cells'
CREATE TABLE [dbo].[Cells] (
    [CellId] uniqueidentifier  NOT NULL,
    [Label] nvarchar(max)  NULL,
    [Description] nvarchar(max)  NOT NULL,
    [x] int  NOT NULL,
    [y] int  NOT NULL,
    [z] int  NOT NULL,
    [CellStateTypeId] int  NOT NULL,
    [CellularAutomataId] int  NOT NULL,
    [TopLeftCellId] uniqueidentifier  NULL,
    [TopCellId] uniqueidentifier  NULL,
    [TopRightCellId] uniqueidentifier  NULL,
    [LeftCellId] uniqueidentifier  NULL,
    [RightCellId] uniqueidentifier  NULL,
    [BottomLeftCellId] uniqueidentifier  NULL,
    [BottomCellId] uniqueidentifier  NULL,
    [BottomRightCellId] uniqueidentifier  NULL,
    [CellCellId] uniqueidentifier  NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [CellStateTypeId] in table 'CellStateTypes'
ALTER TABLE [dbo].[CellStateTypes]
ADD CONSTRAINT [PK_CellStateTypes]
    PRIMARY KEY CLUSTERED ([CellStateTypeId] ASC);
GO

-- Creating primary key on [CellularAutomataId] in table 'CellularAutomatas'
ALTER TABLE [dbo].[CellularAutomatas]
ADD CONSTRAINT [PK_CellularAutomatas]
    PRIMARY KEY CLUSTERED ([CellularAutomataId] ASC);
GO

-- Creating primary key on [ConfigurationId] in table 'Configurations'
ALTER TABLE [dbo].[Configurations]
ADD CONSTRAINT [PK_Configurations]
    PRIMARY KEY CLUSTERED ([ConfigurationId] ASC);
GO

-- Creating primary key on [NeighbourhoodStateConfigurationId] in table 'NeighbourhoodStateConfigurations'
ALTER TABLE [dbo].[NeighbourhoodStateConfigurations]
ADD CONSTRAINT [PK_NeighbourhoodStateConfigurations]
    PRIMARY KEY CLUSTERED ([NeighbourhoodStateConfigurationId] ASC);
GO

-- Creating primary key on [NeighbourhoodTypeId] in table 'NeighbourhoodTypes'
ALTER TABLE [dbo].[NeighbourhoodTypes]
ADD CONSTRAINT [PK_NeighbourhoodTypes]
    PRIMARY KEY CLUSTERED ([NeighbourhoodTypeId] ASC);
GO

-- Creating primary key on [TransitionRuleId] in table 'TransitionRules'
ALTER TABLE [dbo].[TransitionRules]
ADD CONSTRAINT [PK_TransitionRules]
    PRIMARY KEY CLUSTERED ([TransitionRuleId] ASC);
GO

-- Creating primary key on [CellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [PK_Cells]
    PRIMARY KEY CLUSTERED ([CellId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [CellStateTypeId] in table 'NeighbourhoodStateConfigurations'
ALTER TABLE [dbo].[NeighbourhoodStateConfigurations]
ADD CONSTRAINT [FK_NeighbourhoodStateConfigurationCellStateType]
    FOREIGN KEY ([CellStateTypeId])
    REFERENCES [dbo].[CellStateTypes]
        ([CellStateTypeId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NeighbourhoodStateConfigurationCellStateType'
CREATE INDEX [IX_FK_NeighbourhoodStateConfigurationCellStateType]
ON [dbo].[NeighbourhoodStateConfigurations]
    ([CellStateTypeId]);
GO

-- Creating foreign key on [CurrentCellStateTypeId] in table 'TransitionRules'
ALTER TABLE [dbo].[TransitionRules]
ADD CONSTRAINT [FK_TransitionRuleCellStateTypeCurrent]
    FOREIGN KEY ([CurrentCellStateTypeId])
    REFERENCES [dbo].[CellStateTypes]
        ([CellStateTypeId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TransitionRuleCellStateTypeCurrent'
CREATE INDEX [IX_FK_TransitionRuleCellStateTypeCurrent]
ON [dbo].[TransitionRules]
    ([CurrentCellStateTypeId]);
GO

-- Creating foreign key on [ResultCellStateTypeId] in table 'TransitionRules'
ALTER TABLE [dbo].[TransitionRules]
ADD CONSTRAINT [FK_TransitionRuleCellStateTypeResult]
    FOREIGN KEY ([ResultCellStateTypeId])
    REFERENCES [dbo].[CellStateTypes]
        ([CellStateTypeId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TransitionRuleCellStateTypeResult'
CREATE INDEX [IX_FK_TransitionRuleCellStateTypeResult]
ON [dbo].[TransitionRules]
    ([ResultCellStateTypeId]);
GO

-- Creating foreign key on [NeighbourhoodTypeId] in table 'CellularAutomatas'
ALTER TABLE [dbo].[CellularAutomatas]
ADD CONSTRAINT [FK_CellularAutomataNeighbourhoodType]
    FOREIGN KEY ([NeighbourhoodTypeId])
    REFERENCES [dbo].[NeighbourhoodTypes]
        ([NeighbourhoodTypeId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellularAutomataNeighbourhoodType'
CREATE INDEX [IX_FK_CellularAutomataNeighbourhoodType]
ON [dbo].[CellularAutomatas]
    ([NeighbourhoodTypeId]);
GO

-- Creating foreign key on [CellularAutomataId] in table 'TransitionRules'
ALTER TABLE [dbo].[TransitionRules]
ADD CONSTRAINT [FK_CellularAutomataTransitionRule]
    FOREIGN KEY ([CellularAutomataId])
    REFERENCES [dbo].[CellularAutomatas]
        ([CellularAutomataId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellularAutomataTransitionRule'
CREATE INDEX [IX_FK_CellularAutomataTransitionRule]
ON [dbo].[TransitionRules]
    ([CellularAutomataId]);
GO

-- Creating foreign key on [TransitionRuleId] in table 'NeighbourhoodStateConfigurations'
ALTER TABLE [dbo].[NeighbourhoodStateConfigurations]
ADD CONSTRAINT [FK_NeighbourhoodStateConfigurationTransitionRule]
    FOREIGN KEY ([TransitionRuleId])
    REFERENCES [dbo].[TransitionRules]
        ([TransitionRuleId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NeighbourhoodStateConfigurationTransitionRule'
CREATE INDEX [IX_FK_NeighbourhoodStateConfigurationTransitionRule]
ON [dbo].[NeighbourhoodStateConfigurations]
    ([TransitionRuleId]);
GO

-- Creating foreign key on [CellStateTypeId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellCellStateType]
    FOREIGN KEY ([CellStateTypeId])
    REFERENCES [dbo].[CellStateTypes]
        ([CellStateTypeId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellCellStateType'
CREATE INDEX [IX_FK_CellCellStateType]
ON [dbo].[Cells]
    ([CellStateTypeId]);
GO

-- Creating foreign key on [CellularAutomataId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellularAutomataCell]
    FOREIGN KEY ([CellularAutomataId])
    REFERENCES [dbo].[CellularAutomatas]
        ([CellularAutomataId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellularAutomataCell'
CREATE INDEX [IX_FK_CellularAutomataCell]
ON [dbo].[Cells]
    ([CellularAutomataId]);
GO

-- Creating foreign key on [TopCellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellTopCell]
    FOREIGN KEY ([TopCellId])
    REFERENCES [dbo].[Cells]
        ([CellId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellTopCell'
CREATE INDEX [IX_FK_CellTopCell]
ON [dbo].[Cells]
    ([TopCellId]);
GO

-- Creating foreign key on [TopLeftCellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellTopLeftCell]
    FOREIGN KEY ([TopLeftCellId])
    REFERENCES [dbo].[Cells]
        ([CellId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellTopLeftCell'
CREATE INDEX [IX_FK_CellTopLeftCell]
ON [dbo].[Cells]
    ([TopLeftCellId]);
GO

-- Creating foreign key on [TopRightCellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellTopRightCell]
    FOREIGN KEY ([TopRightCellId])
    REFERENCES [dbo].[Cells]
        ([CellId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellTopRightCell'
CREATE INDEX [IX_FK_CellTopRightCell]
ON [dbo].[Cells]
    ([TopRightCellId]);
GO

-- Creating foreign key on [LeftCellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellLeftCell]
    FOREIGN KEY ([LeftCellId])
    REFERENCES [dbo].[Cells]
        ([CellId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellLeftCell'
CREATE INDEX [IX_FK_CellLeftCell]
ON [dbo].[Cells]
    ([LeftCellId]);
GO

-- Creating foreign key on [RightCellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellRightCell]
    FOREIGN KEY ([RightCellId])
    REFERENCES [dbo].[Cells]
        ([CellId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellRightCell'
CREATE INDEX [IX_FK_CellRightCell]
ON [dbo].[Cells]
    ([RightCellId]);
GO

-- Creating foreign key on [BottomLeftCellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellBottomLeftCell]
    FOREIGN KEY ([BottomLeftCellId])
    REFERENCES [dbo].[Cells]
        ([CellId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellBottomLeftCell'
CREATE INDEX [IX_FK_CellBottomLeftCell]
ON [dbo].[Cells]
    ([BottomLeftCellId]);
GO

-- Creating foreign key on [BottomRightCellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellBottomRightCell]
    FOREIGN KEY ([BottomRightCellId])
    REFERENCES [dbo].[Cells]
        ([CellId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellBottomRightCell'
CREATE INDEX [IX_FK_CellBottomRightCell]
ON [dbo].[Cells]
    ([BottomRightCellId]);
GO

-- Creating foreign key on [BottomCellId] in table 'Cells'
ALTER TABLE [dbo].[Cells]
ADD CONSTRAINT [FK_CellBottomCell]
    FOREIGN KEY ([BottomCellId])
    REFERENCES [dbo].[Cells]
        ([CellId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CellBottomCell'
CREATE INDEX [IX_FK_CellBottomCell]
ON [dbo].[Cells]
    ([BottomCellId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------